//
//  FControl.m
//  test
//
//  Created by 袁飞 on 12-7-12.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FListView.h"

@implementation FListView
//@synthesize name = name_;

-(id)initWithContent:(CCLayer*)content{
  [super init];
  
  //[self setColor:ccc3(0, 0, 255)];
  //[self setOpacity:255];
  
  content_ = content;
  [content_ retain];
  
  //[content_ setAnchorPoint:ccp(0, 1)];
  [self addChild:content_];
  
  [self setContentSize:CGSizeMake(100, 320)];
  CGSize s = [self contentSize];
  CGSize cs = [content_ contentSize];

  
  origin_point_ = ccp(cs.width/2, s.height-cs.height/2);
  [content_ setPosition:origin_point_];
  //[content_ setPosition:ccp(0,-280)];

  v_ = ccp(s.height-cs.height/2, cs.height/2);
  h_ = ccp(s.width/2 - abs((s.width-cs.width)/2), s.width/2 + abs((s.width-cs.width)/2));

  //top_left_ = ccp(s.width-cs.width, s.height);
  //top_right_ = ccp(0, s.height);
  //bottom_left_ = ccp();
  //bottom_right_;

  //bound_ = ccp(s.width-cs.width, s.height-cs.height);
  //postion_rect_ = CGRectMake(s.width-cs.width, s.height, abs(s.width-cs.width), abs(s.height-cs.height));
  //postion_rect_ = CGRectMake(s.width/2 - abs((s.width-cs.width)/2), s.height-cs.height, abs(s.width-cs.width), abs(s.height-cs.height));

  //[content_ setAnchorPoint:ccp(0, 1)];
  //[self schedule:(SEL) interval:<#(ccTime)#>]
  return self;
}

-(void)onSlideBegin:(CGPoint)begin End:(CGPoint)end Time:(double)t{
  [self unschedule:@selector(deaccelerateSlide)];
  speed_x_ = 0;
  speed_y_ = 0;
  begin_slide_time_ = 0;
  
  if (t==0) {
    CGPoint p = ccp(end.x-begin.x, end.y-begin.y);
    float x = origin_point_.x+p.x;
    float y = origin_point_.y+p.y;

    
    if (x > h_.y) {
      x = h_.y;
    }
    if (x < h_.x) {
      x = h_.x;
    }
    if (y < v_.x) {
      y = v_.x;
    }
    if (y > v_.y) {
      y = v_.y;
    }

    /*
    CGPoint cp = [content_ position];
    float cx=cp.x,cy=cp.y;
    if (x>h_.x && x<h_.y) {
      cx = origin_point_.x + p.x;
    }
    if (y>v_.x && y<v_.y) {
      cy = origin_point_.y + p.y;
    }*/
    //if(cx!=0)
    [content_ setPosition:ccp(x, y)];
    //NSLog(@"@@@@ set positon %f,%f %f,%f", origin_point_.x+cx, origin_point_.y+cy);
  }else {
    origin_point_ = [content_ position];
    
    speed_x_ = (end.x - begin.x)/t/2;
    speed_y_ = (end.y - begin.y)/t/2;

    if (abs(speed_x_) > 500 || abs(speed_y_) > 500) {
    begin_slide_time_ = [NSDate timeIntervalSinceReferenceDate];
    [self schedule:@selector(deaccelerateSlide) interval:0.02f];
    }
    NSLog(@"deltat %f %f %f", t, speed_x_, speed_y_);
    

    //NSLog(@"@@@@ set origin point");
  }
  //NSLog(@"positon %f %f %f %f", cp.x, cp.y, p.x, p.y);
}

-(void)deaccelerateSlide{
  double end = [NSDate timeIntervalSinceReferenceDate];
  if (end - begin_slide_time_ > 1) {
    [self unschedule:@selector(deaccelerateSlide)];
    return;
  }

  CGPoint p = [content_ position];
  float dx = speed_x_*0.05;
  float dy = speed_y_*0.05;
  
  NSLog(@"deltat %f", speed_y_);

  float x = p.x + dx;
  float y = p.y + dy;
  
  if (x > h_.y) {
    x = h_.y;
  }
  if (x < h_.x) {
    x = h_.x;
  }
  if (y < v_.x) {
    y = v_.x;
  }
  if (y > v_.y) {
    y = v_.y;
  }
  [content_ setPosition:ccp(x, y)];
  origin_point_ = [content_ position];

  /*if (x>h_.x && x<h_.y) {
    [content_ setPosition:ccp(x, p.y)];
  }
  if (y>v_.x && y<v_.y) {
    [content_ setPosition:ccp(p.x, y)];
  }*/

  
  //speed_x_ -= 10;
  //speed_y_ -= 10;

  
  if (speed_x_ < 0) {
    speed_x_ += (speed_x_/50);
  }else {
    speed_x_ -= (speed_x_/50);
  }
  
  if (speed_y_ < 0) {
    speed_y_ += abs(speed_y_/50);
    if (speed_y_ > 0) {
      speed_y_ = 0;
    }
  }else if(speed_y_ > 0) {
    speed_y_ -= abs(speed_y_/50);
    if (speed_y_ < 0) {
      speed_y_ = 0;
    }
  }

  //float x = origin_point_.x+p.x;
  //float y = origin_point_.y+p.y;

  
//-(void)deaccelerateSlide{
//double time_end = [NSDate timeIntervalSinceReferenceDate];
//NSLog(@"time --> %f", time_end);
}

-(void)beforeDraw {
		
    CGPoint test =  self.parent.position;
    
		// TODO: This scrollview should respect parents' positions
		CGPoint screenPos = [self convertToWorldSpace:self.parent.position];
    
    glEnable(GL_SCISSOR_TEST);
    float s = [self scale];

#ifdef __CC_PLATFORM_IOS
    CCDirectorIOS *director = (CCDirectorIOS *) [CCDirector sharedDirector];
    s *= [director contentScaleFactor];
#endif

    CGSize viewSize = [self contentSize];
    glScissor(screenPos.x*s, screenPos.y*s, viewSize.width*s, viewSize.height*s);
  
    //glScissor(200, 400, viewSize.width*s, viewSize.height*s);

    //NSLog(@"draw %f %f %f %f %f", screenPos.x, screenPos.y, viewSize.width, viewSize.height, s);
    //glScissor(screenPos.x, screenPos.y+55, viewSize_.width*s, viewSize_.height*s);
}

-(void)afterDraw {
  glDisable(GL_SCISSOR_TEST);
}
-(void) visit
{
	// quick return if not visible
	//if (!visible_)
	//	return;
	kmGLPushMatrix();
	
  //	glPushMatrix();
	
	if ( grid_ && grid_.active) {
		[grid_ beforeDraw];
		[self transformAncestors];
	}
	[self transform];
  [self beforeDraw];
	if(children_) {
		ccArray *arrayData = children_->data;
		NSUInteger i=0;
		
		// draw children zOrder < 0
		for( ; i < arrayData->num; i++ ) {
			CCNode *child =  arrayData->arr[i];
			if ( [child zOrder] < 0 ) {
				[child visit];
			} else
				break;
		}
		
		// self draw
		[self draw];
		
		// draw children zOrder >= 0
		for( ; i < arrayData->num; i++ ) {
			CCNode *child =  arrayData->arr[i];
			[child visit];
		}
    
	} else
		[self draw];
	
  [self afterDraw];
	if ( grid_ && grid_.active)
		[grid_ afterDraw:self];
	
	kmGLPopMatrix();
	
  //	glPopMatrix();
}
@end
